package cn.com.ddapp.ddm.ui.mine;


import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.zhy.autolayout.AutoLinearLayout;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.SPUtils;
import okhttp3.Call;

public class LoginPasswordActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.et_old)
    EditText etOld;
    @BindView(R.id.et_new)
    EditText etNew;
    @BindView(R.id.et_newok)
    EditText etNewok;
    @BindView(R.id.submit)
    Button submit;

    String old, news, newsOk;
    String userId;
    @BindView(R.id.et1L)
    AutoLinearLayout et1L;
    @BindView(R.id.et2L)
    AutoLinearLayout et2L;
    @BindView(R.id.et3L)
    AutoLinearLayout et3L;
    int orientation;

    @Override
    public void initView() {
        titletext.setText(R.string.alter_loginpass);
        //判断是否横屏,1竖屏0横屏
        orientation = getResources().getConfiguration().orientation;
        if (orientation==0||orientation==2){
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int screenWidth = dm.widthPixels;
            int screenHeigh = dm.heightPixels;
            ViewGroup.LayoutParams params = et1L.getLayoutParams();
            params.height = screenHeigh/10;
            et1L.setLayoutParams(params);
            et2L.setLayoutParams(params);
            et3L.setLayoutParams(params);
        }
    }

    @Override
    public void initData() {
        userId = SPUtils.getStringData(this, SPUtils.USERID);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_login_password;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.back, R.id.submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.submit:
                old = etOld.getText().toString();
                news = etNew.getText().toString();
                newsOk = etNewok.getText().toString();
                if (old.equals("")) {
                    BaseApplication.getIntstance().showToast("原登录密码不能为空", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    return;
                }
                if (news.equals("")) {
                    BaseApplication.getIntstance().showToast("请输入新密码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    return;
                }
                if (newsOk.equals("")) {
                    BaseApplication.getIntstance().showToast("请再次确认密码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    return;
                }
                if (!news.equals(newsOk)) {
                    BaseApplication.getIntstance().showToast("您输入的新密码不一致", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    etNew.setText("");
                    etNewok.setText("");
                    return;
                }
                old = etOld.getText().toString();
                news = etNew.getText().toString();
                newsOk = etNewok.getText().toString();
                OkUtilCallback callback = new OkUtilCallback() {
                    @Override
                    public void onMyResponse(String response, int id) {
                        Gson gson = new Gson();
                        Entity entity = gson.fromJson(response, Entity.class);
                        if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                            BaseApplication.getIntstance().showToast("请登录", Toast.LENGTH_SHORT, 0, Gravity.TOP);
                        } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                            BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                        } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                            BaseApplication.getIntstance().showToast("修改成功", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        BaseApplication.getIntstance().showToast(e.toString(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    }
                };
                /*userId [Y] 会员ID
                oldPwd [Y] 旧密码
                pwd string[Y] 密码
                confirmPwd string[Y] 确认密码*/
                OKUtilAPI.get(OKUtilAPI.alterLoginPwd, "userId=" + userId + "&oldPwd=" + old + "&pwd=" + news + "&confirmPwd=" + newsOk, callback);
                break;
        }
    }
}
